package com.iss.dao.imp;

import com.iss.dao.RoomDAO;
import com.iss.model.Room;
import com.iss.util.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoomDAOImp implements RoomDAO {

    private DBUtil dbUtil = new DBUtil();

    @Override
    public Room queryByNO(String RoomNO) {
        return null;
    }

    @Override
    public Boolean insert(Room room) {
        String sql = "INSERT INTO `hotel`.`room`\n" +
                "(`roomNO`,\n" +
                "`status`,\n" +
                "`roomType`,\n" +
                "`price`)\n" +
                "VALUES(?,\n" +
                "?,\n" +
                "?,\n" +
                "?);";
        int num = dbUtil.update(sql, room.getRoomNO()
                , room.getStatus(), room.getRoomType(), room.getPrice());
        return num > 0;
    }

    @Override
    public Boolean update(Room room) {
        /*UPDATE `hotel`.`room`
        SET
`roomid` = <{roomid: }>,
`roomNO` = <{roomNO: }>,
`status` = <{status: }>,
`roomType` = <{roomType: }>,
`price` = <{price: }>
WHERE `roomid` = <{expr}>;*/
        return null;
    }

    @Override
    public Boolean delete(Room room) {
        return null;
    }

    @Override
    public List<Room> query() {
        List<Room> list = new ArrayList<>();
        String sql = "select * from room";
        ResultSet rs = dbUtil.query(sql);
        try {
            while (rs.next()) {
                list.add(getRoomModel(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    private Room getRoomModel(ResultSet rs) {
        Room room = new Room();
        try {
            room.setRoomNO(rs.getString("roomNO"));
            room.setPrice(rs.getDouble("price"));
            room.setRoomType(rs.getInt("roomType"));
            room.setStatus(rs.getInt("status"));

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return room;
    }

    @Override
    public Boolean exists(Room room) {
        return null;
    }
}
